<template>
  <div class="item-content">
    <div class="form-content" style="margin-bottom: 20px">
      <div class="item-header">
        <div class="subheading">外培计划持证统计表</div>
        <div>
          <el-button
            type="success"
            size="small"
            @click="
              statisticsVisible = true;
              clearLoading();
            "
            >新增</el-button
          >
        </div>
      </div>
      <!-- 表格展示 -->
      <div>
        <el-table height="600" :data="statisticsTableData" border>
          <template #empty>
            <el-empty
              v-show="!statisticsTableData.length"
              description="描述文字"
            >
            </el-empty>
          </template>
          <el-table-column prop="name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="post" label="岗位" width="180">
          </el-table-column>
          <el-table-column prop="trainingName" label="培训名称">
          </el-table-column>
          <el-table-column prop="results" label="考核结果">
            <!-- {{tableData.remarks}} -->
          </el-table-column>
          <el-table-column
            prop="forensicNumber"
            label="培训取证号码"
          ></el-table-column>
          <el-table-column prop="expirationDate" label="复审有效期" width="250">
            <template #default="{ row }">
              <el-date-picker
                v-show="row.expirationDate"
                value-format="yyyy-MM-ddThh:mm:ss"
                disabled
                v-model="row.expirationDate"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column prop="remind" label="到期前提醒">
            <template #default="{ row }">
              <el-radio-group v-model="row.remind" disabled>
                <el-radio :label="1" size="large">是</el-radio>
                <el-radio :label="2" size="large">否</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column prop="certificate" label="证书照片">
            <template #default="{ row }">
              <el-link
                type="primary"
                :href="row.certificate"
                target="blank"
                v-show="row.certificate != null"
                >查看</el-link
              >
            </template>
          </el-table-column>
          <el-table-column width="120" label="操作">
            <template #default="{ row }">
              <el-popconfirm
                style="margin-right: 10px"
                title="确定删除吗？"
                @confirm="deleteSafetyRow(row.id)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  size="large"
                  icon="el-icon-delete"
                  circle
                ></el-button>
              </el-popconfirm>
              <el-button
                @click="editSafetyRow(row.id)"
                type="primary"
                icon="el-icon-edit"
                circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div
      style="
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
      "
    >
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 15, 50, 100, 200, 300, 400, 1000]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <!-- loading -->
    <el-dialog
      title="外培计划特证统计表信息填写"
      :visible.sync="statisticsVisible"
      width="50%"
      center
    >
      <el-form :model="statisticsDialogForm" label-width="auto">
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="姓名：" prop="name">
              <el-input
                placeholder="请输入姓名"
                v-model="statisticsDialogForm.name"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位" prop="post">
              <el-input
                placeholder="请输入岗位"
                v-model="statisticsDialogForm.post"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训名称" prop="trainingName">
              <el-input
                placeholder="请输入培训名称"
                v-model="statisticsDialogForm.trainingName"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核结果" prop="results">
              <el-input
                placeholder="请输入培训名称"
                v-model="statisticsDialogForm.results"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="培训取证号码" prop="forensicNumber">
              <el-input
                placeholder="请输入培训取证号码"
                v-model="statisticsDialogForm.forensicNumber"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="复审有效期" prop="expirationDate">
              <el-date-picker
                v-model="statisticsDialogForm.expirationDate"
                type="date"
                value-format="yyyy-MM-ddThh:mm:ss"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到期前提醒" prop="remind">
              <el-radio-group v-model="statisticsDialogForm.remind">
                <el-radio :label="1" size="large">是</el-radio>
                <el-radio :label="2" size="large">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="证书扫描件链接" prop="certificateName">
              <el-input
                placeholder="请输入证书扫描件链接"
                v-model="statisticsDialogForm.certificateLinks"
              >
              </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="证书照片" prop="certificate">
              <el-upload
                v-model="statisticsDialogForm.certificate"
                :file-list="fileList4"
                :on-success="safetyResponsibilityLetterSuccess"
                :on-remove="safetyResponsibilityLetterRemove"
                class="upload-demo"
                action="/api/upload"
                :on-error="fileError"
                :limit="limit"
              >
                <el-button type="primary">上传证书</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSecurity()">确 定</el-button>
        <el-button @click="statisticsVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { defineProps, defineComponent, onBeforeMount } from "vue";
import { ref } from "vue";
import { Notification } from "element-ui";
import {
  addStatistics,
  deleteStatistics,
  reviseStatistics,
  searchStatistics,
} from "@/api/statistics";
const fileList4 = ref([]);
const total = ref(0);
const page = ref(1);
const size = ref(10);
const loading = ref(true);
const limit = ref(1);
const props = defineProps();
const message = "Hello, Vue 3!";
const NewComponentName = defineComponent({
  name: "StatisticsA",
  setup() {
    return {
      message,
    };
  },
});
// 使用props和NewComponentName，以避免ESLint报错
console.log(props);
console.log(NewComponentName);

// 安全责任书移除
const safetyResponsibilityLetterRemove = (file) => {
  console.log(file.url);
  fileList4.value.map((item, index) => {
    if (item.url == file.url) {
      fileList4.value.splice(index, 1);
    }
  });
};
//安全责任书文件上传
const safetyResponsibilityLetterSuccess = (res) => {
  if (res.code == 0) {
    Notification({
      title: "成功",
      message: "上传成功",
      type: "success",
    });
    fileList4.value.push({
      name: res.data[1],
      url: "api/" + res.data[0],
    });
    statisticsDialogForm.value.certificate = "api/" + res.data[0];
  } else {
    Notification({
      title: "失败",
      message: "上传失败",
      type: "warning",
    });
  }
};
const fileError = () => {
  Notification({
    title: "失败",
    message: "上传失败",
    type: "warning",
  });
};
const changeTable = async () => {
  loading.value = true;
  let result = await searchStatistics(page.value, size.value);
  if (result?.code == 0) {
    statisticsTableData.value = result?.data;
    total.value = Number(result?.message);
    if (isNaN(total.value)) {
      total.value = 0;
    }
    loading.value = false;
  } else {
    loading.value = false;
    statisticsTableData.value = [];
  }
};
const handleSizeChange = async (val) => {
  size.value = val;
  page.value = 1;
  changeTable();
};
const handleCurrentChange = async (val) => {
  page.value = val;
  changeTable();
};
onBeforeMount(async () => {
  changeTable();
});
//单选框
const statisticsVisible = ref(false);
//dialog表单
const statisticsDialogForm = ref({
  name: "", //姓名
  post: "", //岗位
  trainingName: "", //培训名称
  results: "", //考核结果
  forensicNumber: "", //培训取证号码
  expirationDate: null, //复审有效期
  remind: 2, // 是否到期提醒
  certificate: null, //证书照片
});
const statisticsTableData = ref([
  // {
  //   id: 1,
  //   name: "姓名",
  //   post: "岗位",
  //   trainingName: "培训名称",
  //   results: "考核结果",
  //   forensicNumber: "培训取证号码",
  //   expirationDate: "复审有效期",
  //   remind: 1, // 是否到期提醒
  //   certificate: "证书照片",
  // },
]); //编辑行 安全
const editSafetyRow = (id) => {
  const item = statisticsTableData.value.find((item) => item.id == id);
  if (item) {
    fileList4.value = [];
    statisticsDialogForm.value = { ...item };
    statisticsDialogForm.value.type = "edit";
    statisticsDialogForm.value.certificate != null
      ? fileList4.value.push({
          name: statisticsDialogForm.value.certificate,
        })
      : "";
    statisticsVisible.value = true;
  } else {
    console.error("Item with id " + id + " not found.");
  }
};

const deleteSafetyRow = async (id) => {
  let result = await deleteStatistics(id);
  if (result.code == 0) {
    Notification({
      title: "成功",
      message: "删除成功",
      type: "success",
    });
    changeTable();
  } else {
    Notification({
      title: "失败",
      message: "删除失败",
      type: "error",
    });
  }
};
const submitSecurity = async () => {
  statisticsVisible.value = false;
  if (statisticsDialogForm.value.type === "edit") {
    fileList4.value.length == 0
      ? (statisticsDialogForm.value.certificate = null)
      : "";
    let result = await reviseStatistics(statisticsDialogForm.value);
    console.log(statisticsDialogForm.value);
    if (result.code == 0) {
      Notification({
        title: "成功",
        message: "修改成功",
        type: "success",
      });
      changeTable();
    } else {
      Notification({
        title: "失败",
        message: "修改失败",
        type: "error",
      });
    }
  } else {
    // 添加新项
    let result = await addStatistics(statisticsDialogForm.value);
    if (result?.code == 0) {
      Notification({
        title: "成功",
        message: "添加成功",
        type: "success",
      });
      changeTable();
    } else {
      Notification({
        title: "失败",
        message: "添加失败",
        type: "error",
      });
    }
  }
  // 重置 statisticsDialogForm 为初始状态
  clearLoading();
};

const clearLoading = () => {
  //dialog表单
  statisticsDialogForm.value = {
    name: "", //姓名
    post: "", //岗位
    trainingName: "", //培训名称
    results: "", //考核结果
    forensicNumber: "", //培训取证号码
    expirationDate: null, //复审有效期
    remind: 1,
    certificate: null, //证书照片
  };
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  padding: 10em;
}

.el-range-editor.el-input__inner {
  width: 240px;
}

:deep(.el-input__inner) {
  height: auto !important;
}
// :deep(.cell) {
//   display: flex;
//   justify-content: space-around;
// }
::v-deep .el-upload-list__item {
  transition: none !important;
}
::v-deep .el-upload-list__item-thumbnail {
  /* 图片在方框内显示长边 */
  object-fit: scale-down !important;
}

:deep(.el-input__inner) {
  height: auto !important;
}
:deep(.el-icon-arrow-right:before) {
  font-size: 12px !important;
}
:deep(.el-table th) {
  background: #fafafa;
}
:deep(.el-collapse-item__header) {
  font-weight: bold;
  color: #333;
  font-size: 15px;
  padding: 0 10px;
}
:deep(.el-icon-arrow-right:before) {
  font-weight: bold;
  color: #333;
  font-size: 25px;
}
body {
  min-height: 100vh;

  transition: all 0.3s ease;
}
.container {
  height: 100%;
  padding: 15px;
  overflow: auto;
  box-sizing: border-box;

  .main-content {
    background-color: #fff;
    border: 1px solid #e6e6e6;
    border-radius: 6px;
    min-width: 700px;

    .header-container {
      padding: 15px 15px;
      border-bottom: solid 1px #e6e6e6;
      box-sizing: border-box;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;

      .title {
        position: relative;
        font-weight: bold;
        color: #333;
        font-size: 15px;
        padding-left: 16px;

        &::before {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }

        &::after {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }
      }
    }
  }
}

.main {
  padding: 12px 15px;

  .item-content {
    height: 100%;
    overflow: hidden;
    border-radius: 4px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;

    .item-header {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;

      .subheading {
        position: relative;
        color: #555;
        font-size: 14px;
        padding-left: 14px;

        &::after {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 0;
          top: calc(50% - 7px);
          position: absolute;
        }

        &::before {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 4px;
          top: calc(50% - 7px);
          position: absolute;
        }
      }
    }

    .content-container {
      margin-top: -1px;
    }
  }
}

.w-100 {
  width: 100%;
}

.item-form-content {
  display: flex;
  flex-direction: row;
  align-items: center;

  i {
    margin: 0 10px;
  }
}

.productList-content {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  height: 40px;
}

.form-content {
  padding: 0 10px;
}

.dialog-action-bar {
  text-align: center;
  margin-top: 30px;
}
</style>
